﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：局信息
//********************************************************************************//
using System;
using System.Web.UI;
using Entity;
using BLL;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui.JiChuXX
{
    public partial class JuXinX : BasePage
    {
        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int JuXinXID
        {
            get
            {
                return Convert.ToInt32(ViewState["_JuXinXID"]);
            }
            set
            {
                ViewState["_JuXinXID"] = value;
            }
        }

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource2
        {
            get
            {
                return ViewState["dtSource2"] as DataTable;
            }
            set
            {
                ViewState["dtSource2"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 局信息对象
        /// </summary>
        YS_JX_JUXINX _YS_JX_JUXINX = new YS_JX_JUXINX();

        /// <summary>
        /// 局信息BLL
        /// </summary>
        YS_JX_JUXINXBLL _YS_JX_JUXINXBLL = new YS_JX_JUXINXBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                
                //SetData();
            }
                LoadData();
            ////每次回发时从内存中取得数据源重新绑定,否则列头无法排序
            //this.gvList.DataSource = this.dtSource;
            //this.gvList.DataBind();
            //this.gvGongSi.DataSource = this.dtSource2;
            //this.gvGongSi.DataBind();
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {            
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));           
            /*修改本月则下月同时修改 修改下月只则修改下月*/
            if (e.Parameter == "save")
            {
                OperateState operateState;
                //添加
                if (id < 0)
                {
                    //判断局编号是否重复
                    if (_YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(string.Format("S_BIANHAO='{0}'", this.txtJuBH.Text), null).Rows.Count > 0)
                    {
                        e.Result = e.Parameter + "|-1|你所输入的局编号,系统中已经存在使用过,请修改！";
                        return;
                    }
                    operateState = OperateState.Add;
                    _YS_JX_JUXINX.ID = 0;
                    _YS_JX_JUXINX.I_JLZT = 0;
                    //添加下月的数据
                    _YS_JX_JUXINX.I_LEIXING = 1;
                }
                //修改
                else
                {
                    //修改操作
                    operateState = OperateState.Modify;
                    _YS_JX_JUXINX = this._YS_JX_JUXINXBLL.SelectYS_JX_JUXINX(id);
                }
                _YS_JX_JUXINX.S_BIANHAO = this.txtJuBH.Text;
                _YS_JX_JUXINX.S_MINGCHENG = this.txtJuMingC.Text;
                _YS_JX_JUXINX.S_DIZHI = this.txtJuDiZ.Text;
                _YS_JX_JUXINX.S_YOUBIAN = this.txtYouBian.Text;
                _YS_JX_JUXINX.S_LIANXIDH = this.txtLianXiDH.Text;
                _YS_JX_JUXINX.S_LIANXIBM = this.txtLianXiBM.Text;
                _YS_JX_JUXINX.S_LIANXIR = this.txtLianXiR.Text;
                _YS_JX_JUXINX.I_YEWUY = Convert.ToInt32(this.ddlYeWuY.Value);
                _YS_JX_JUXINX.S_ZHANDIANBH = Convert.ToString(this.conZhanDian.Value);
                _YS_JX_JUXINX.S_BEIZHU = this.txtBeiZhu.Text;
                //设置操作信息
                this._YS_JX_JUXINX.I_XIUGAIR = LoginInfo.CurrentUser.UserID.ToString();
                this._YS_JX_JUXINX.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                this._YS_JX_JUXINX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                if (operateState == OperateState.Add)
                {
                    _SYS_LOGBLL.Log<YS_JX_JUXINX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "局信息", operateState, _YS_JX_JUXINX, null);
                    _YS_JX_JUXINXBLL.InsertOrUpdate(_YS_JX_JUXINX);
                }
                else
                {
                    //如果修改
                    _YS_JX_JUXINXBLL.UpdateByLeixing(_YS_JX_JUXINX, this.conNianYu.Text == "下一月" ? (int?)1 : null);
                }
               
                e.Result = e.Parameter + "|0|保存成功！";
                return;
            }
            else if (e.Parameter == "deleteValidate")
            {
                _YS_JX_JUXINX = _YS_JX_JUXINXBLL.SelectYS_JX_JUXINX(id);
                //确认是否有用户使用
                if (new YS_YG_YONGHUXXBLL().SelectYS_YG_YONGHUXXesDynamic(string.Format("s_jubianh='{0}'", _YS_JX_JUXINX.S_BIANHAO), null).Rows.Count > 0)
                {
                    e.Result = e.Parameter + "|-1|当前局信息正在使用,不允许删除！";
                }
                else
                {
                    e.Result = e.Parameter + "|0|success";
                }
            }
            else if (e.Parameter == "delete")
            {
                //执行删除
                _YS_JX_JUXINX = _YS_JX_JUXINXBLL.SelectYS_JX_JUXINX(id);
                this._YS_JX_JUXINX.I_XIUGAIR = LoginInfo.CurrentUser.UserID.ToString();
                this._YS_JX_JUXINX.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                //this._YS_JX_JUXINX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                this._YS_JX_JUXINX.I_JLZT = -1;
                _YS_JX_JUXINXBLL.UpdateByLeixing(_YS_JX_JUXINX, this.conNianYu.Text == "下一月" ? (int?)1 : null);
                //if (_YS_JX_JUXINXBLL.InsertOrUpdate(_YS_JX_JUXINX, null))
                //{
                _SYS_LOGBLL.Log<YS_JX_JUXINX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "局信息", OperateState.Delete, _YS_JX_JUXINX, null);
                e.Result = e.Parameter + "|0|删除成功！";
                //}
            }
            else if (e.Parameter == "search")
            {
                //要定位的记录局编号
                string conBH = this.conJuBH.Text.Trim();
                //重新绑定下结果集,否则结果集并不是最新的
                LoadData();
                //定位
                if (conBH != "")
                {
                    int index1 = PageTools.SearchRecordIndex(this.gvList, "S_BIANHAO", conBH, false);
                    int index2 = PageTools.SearchRecordIndex(this.gvList, "S_MINGCHENG", conBH, false);
                    if (index1 == -1 && index2 == -1)
                    {
                        e.Result = e.Parameter + "|-1|未找到对应数据！";
                    }
                    else
                    {
                        int rsindex = -1;
                        if (index1 == -1)
                        {
                            rsindex = index2;
                        }
                        else if (index2 == -1)
                        {
                            rsindex = index1;
                        }
                        else
                        {
                            rsindex = index1 < index2 ? index1 : index2;
                        }
                        e.Result = e.Parameter + "|0|" + rsindex;
                    }
                }
            }//"ID;S_BIANHAO;S_MINGCHENG;S_DIZHI;S_YOUBIAN;S_LIANXIDH;S_LIANXIBM;S_LIANXIR;I_YEWUY;S_BEIZHU"
            else if (e.Parameter == "setinfo")
            {
                string msg = "";
                if (this.conNianYu.SelectedIndex < 2)
                {
                    this._YS_JX_JUXINX = this._YS_JX_JUXINXBLL.SelectYS_JX_JUXINX(id);
                    msg = string.Join("|", this._YS_JX_JUXINX.S_BIANHAO, this._YS_JX_JUXINX.S_MINGCHENG, this._YS_JX_JUXINX.S_DIZHI, this._YS_JX_JUXINX.S_YOUBIAN, this._YS_JX_JUXINX.S_LIANXIDH, this._YS_JX_JUXINX.S_LIANXIBM, this._YS_JX_JUXINX.S_LIANXIR, this._YS_JX_JUXINX.I_YEWUY, this._YS_JX_JUXINX.S_BEIZHU);
                }
                else
                {
                    DataRow drtemp = BusinessTool.GETDATAHISTORY("YS_JX_JUXINX", this.conLiShiNY.Text, "ID=" + id).Rows[0];
                    msg = string.Join("|", Convert.ToString(drtemp["S_BIANHAO"]), Convert.ToString(drtemp["S_MINGCHENG"]), Convert.ToString(drtemp["S_DIZHI"]), Convert.ToString(drtemp["S_YOUBIAN"]), Convert.ToString(drtemp["S_LIANXIDH"]), Convert.ToString(drtemp["S_LIANXIBM"]), Convert.ToString(drtemp["S_LIANXIR"]), Convert.ToString(drtemp["I_YEWUY"]), Convert.ToString(drtemp["S_BEIZHU"]));
                }
                e.Result = e.Parameter + "|0|" + msg;
            }
        }

        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerform_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 加载局信息
        /// </summary>
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 加载公司信息
        /// </summary>
        protected void gvGongSi_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            //加载下属公司信息
            //非历史数据
            if (this.conNianYu.SelectedIndex < 2)
            {
                BLL.YS_JX_GONGSIXXBLL bllT = new YS_JX_GONGSIXXBLL();
                this.dtSource2 = bllT.SelectYS_JX_GONGSIXXesDynamic(string.Format("s_jubianh='{0}'", this.txtJuBH.Text), null);
            }
            else
            {
                //历史数据
                this.dtSource2 = BusinessTool.GETDATAHISTORY("YS_JX_GONGSIXX", this.conLiShiNY.Text, string.Format("S_JUBIANH='{0}'", this.txtJuBH.Text));
            }
            this.gvGongSi.DataSource = this.dtSource2;
            this.gvGongSi.DataBind();

        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            PageTools.LoadCombox(this.conZhanDian, PageTools.eLoadType.站点, BusinessTool.ZHANDIANBH(), null, null);
            //加载业务员
            PageTools.LoadCombox(this.ddlYeWuY, PageTools.eLoadType.业务员, BusinessTool.ZHANDIANBH(), null, null);
            //隐藏历史年月文本框
            this.conLiShiNY.ClientVisible = false;
            //获取当前水量年月
            this.hdCollection.Set("YM", BusinessTool.GetWaterYM());
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.txtBeiZhu.Text = "";
            this.txtLianXiDH.Text = "";
            this.txtJuBH.Text = "";
            this.txtJuDiZ.Text = "";
            this.txtJuMingC.Text = "";
            this.txtLianXiBM.Text = "";
            this.txtLianXiR.Text = "";
            this.txtYouBian.Text = "";
            this.lblCountTip.Text = "";
            this.ddlYeWuY.Value = "";
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            //查询站点下所有局
            string condition = "";
            condition = string.Format("s_zhandianbh='{0}'", this.conZhanDian.Value);
            //查询的是本月或下月
            if (Convert.ToInt32(this.conNianYu.Value) == 0 || Convert.ToInt32(this.conNianYu.Value) == 1)
            {
                condition = string.Format("{0} and I_LEIXING={1}", condition, this.conNianYu.Value);
                this.dtSource = _YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(condition, "ID");
            }
            else
            {
                if (this.conLiShiNY.Text == "")
                {
                    this.dtSource = null;
                }
                else
                {
                    //查询的是历史数据
                    this.dtSource = BusinessTool.GETDATAHISTORY("YS_JX_JUXINX", conLiShiNY.Text, string.Format("S_ZHANDIANBH='{0}'", this.conZhanDian.Value));
                }
            }
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            
        }

        ///// <summary>
        ///// 给输入框赋值
        ///// </summary>
        //private void SetData()
        //{
        //    if (this.gvList.FocusedRowIndex == -1)
        //    {
        //        InitData();
        //        return;
        //    }
        //    int id = Convert.ToInt32(this.gvList.GetRowValues(this.gvList.FocusedRowIndex, "ID"));
        //    this.hdCollection.Set("ID", id);
        //    if (this.conNianYu.SelectedIndex < 2)
        //    {
        //        this._YS_JX_JUXINX = this._YS_JX_JUXINXBLL.SelectYS_JX_JUXINX(id);
        //        this.txtBeiZhu.Text = _YS_JX_JUXINX.S_BEIZHU;
        //        this.txtLianXiDH.Text = _YS_JX_JUXINX.S_LIANXIDH;
        //        this.txtJuBH.Text = _YS_JX_JUXINX.S_BIANHAO;
        //        this.txtJuDiZ.Text = _YS_JX_JUXINX.S_DIZHI;
        //        this.txtJuMingC.Text = _YS_JX_JUXINX.S_MINGCHENG;
        //        this.txtLianXiBM.Text = _YS_JX_JUXINX.S_LIANXIBM;
        //        this.txtLianXiR.Text = _YS_JX_JUXINX.S_LIANXIR;
        //        this.txtYouBian.Text = _YS_JX_JUXINX.S_YOUBIAN;
        //        this.ddlYeWuY.Value = _YS_JX_JUXINX.I_YEWUY.ToString();
        //    }
        //    else
        //    {
        //        if (this.dtSource != null)
        //        {
        //            foreach (DataRow row in this.dtSource.Rows)
        //            {
        //                if (Convert.ToInt32(row["ID"]) == this.JuXinXID)
        //                {
        //                    this.txtBeiZhu.Text = Convert.ToString(row["S_BEIZHU"]);
        //                    this.txtLianXiDH.Text = Convert.ToString(row["S_LIANXIDH"]);
        //                    this.txtJuBH.Text = Convert.ToString(row["S_BIANHAO"]);
        //                    this.txtJuDiZ.Text = Convert.ToString(row["S_DIZHI"]);
        //                    this.txtJuMingC.Text = Convert.ToString(row["S_MINGCHENG"]);
        //                    this.txtLianXiBM.Text = Convert.ToString(row["S_LIANXIBM"]);
        //                    this.txtLianXiR.Text = Convert.ToString(row["S_LIANXIR"]);
        //                    this.txtYouBian.Text = Convert.ToString(row["S_YOUBIAN"]);
        //                    this.ddlYeWuY.Value = row["I_YEWUY"].ToString();
        //                }
        //            }
        //        }
        //    }
        //}

        #endregion
    }
}